<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/08/0008
 * Time: 15:51
 */

namespace app\sports_admin\model;


use think\Model;
use traits\model\SoftDelete;

class SchoolYear extends Model
{
    use SoftDelete;
    protected $autoWriteTimestamp = true;

    protected $pk = 'id';

    protected $table = 'school_year';

    public function getSemesterAttr($value)
    {
        return $value == 2 ? '下学期' : '上学期';
    }

    public function school()
    {
        return $this->belongsTo('school','school_id','school_id')->field('school_id,school_name');
    }
    /*
    * 获取学年信息
    * @param $page 第几页
    * @param $rows 每页显示的条数
    * @param $sort 要排序的字段
    * @param $sortOrder 升序/降序
    * @param $searchName 要检索的字段
    * @param $schoolId 学校id
    */
    public static function getSchoolYearInfo($page,$rows,$sort,$sortOrder,$searchName,$schoolId)
    {
        $result = self::with('school')->where(function($query)use($searchName,$schoolId){
            if($searchName){
                $query->where('section','like',"%{$searchName}%");
            }
            if($schoolId){
                $query->where('school_id',$schoolId);
            }
        })->order("{$sort}","{$sortOrder}")->limit($rows)->page($page)->select();
        $count = self::with('school')->where(function($query)use($searchName,$schoolId){
            if($searchName){
                $query->where('section','like',"%{$searchName}%");
            }
            if($schoolId){
                $query->where('school_id',$schoolId);
            }
        })->count();
        return json(['rows'=>$result,'total'=>$count]);
    }
    /*
     * 添加学年信息
     * @param array $input 学年信息
     */
    public static function insertSchoolYearInfo($input)
    {
        return self::create($input);
    }
}